<?php
/**
 * Created by PhpStorm.
 * User: USER
 * Date: 2017/4/26
 * Time: 9:40
 */

namespace JsonRpc\Services\Order;


use JsonRpc\Models\Req;
use JsonRpc\Models\ReqLine;
use JsonRpc\Models\ReqLog;
use JsonRpc\Services\BaseService;
use JsonRpc\Services\CommonService;
use JsonRpc\Services\System\UserService;
use JsonRpc\Utils\Utils;

class RequisitionService extends BaseService
{
    use CommonService;

    private static function getBaseModel(){
        return "JsonRpc\\Models\\Req";
    }

    //获取认购单列表
    public static function getReqList($params)
    {
        $goods_num = 0;
        $order_num = 0;
        $res = self::getInfoList($params);
        foreach ($res['rows'] as $key => $val) {
            $r = ReqLine::where('req_id',$val['id'])->get();
            foreach ($r as $k => $v) {
                $order_num += $v['order_num'];
                $goods_num += $v['goods_num'];
            }
            $res['rows'][$key]['goods_num_total'] = $goods_num;
            $res['rows'][$key]['order_num_total'] = $order_num;
        }
        if (!empty($res)) {
            return $res;
        }
        return [];
    }

    //获取单个认购单信息
    public static function getReqOne($params)
    {
        $data = [];
        $res = Req::where('id',$params['req_id'])->first();
//        $data['req_id'] = $res['id'];
//        $r = ReqLine::where('req_id',$res['id'])->get();
//        foreach ($r as $k => $v) {
//            $data['goods'][$k]['goods_id'] = $v['goods_id'];
//            $data['goods'][$k]['goods_name'] = $v['goods_name'];
//            $data['goods'][$k]['goods_price'] = $v['goods_price'];
//            $data['goods'][$k]['goods_num'] = $v['goods_num'];
//        }
        if (!empty($res)) {
            return $res;
        }
        return [];
    }

    //获取单挑认购单商品信息
    public static function getReqGoods($params)
    {
        $res = ReqLine::where('req_id',$params['id'])->get();
        if (!empty($res)) {
            return $res;
        }
        return [];
    }

    //保存认购单信息
    public static function saveReqInfo($params)
    {
        if(isset($params['id']) && $params['id']){
            //更新
            $model = Req::find($params['id']);
            $model->updated_by = $params['curr_user_id'];
        }else{
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            //新增
            unset($params['id']);
            $model = new Req();
            $model->client_id = $user['client_id'];
            $model->org_id = $user['org_id'];
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
            $model->sn = Utils::createSn($params['id']);
        }
        $model->customer_id = $params['customer_id'];
        $model->customer_mobile = $params['customer_mobile'];
        $model->customer_name = $params['customer_name'];
//        $model->description = $params['description'];
        $model->warehouse_id = $params['warehouse_id'];
        $model->shipping_methods = $params['shipping_methods'];
        $model->shipping_address = $params['shipping_address'];
        self::beginTransaction();
        $model->save();
        ReqLog::insert([
            'client_id' => $model->client_id,
            'org_id' => $model->org_id,
            'created_by' => $model->created_by,
            'updated_by' => $model->updated_by,
            'req_id' => $model->id,
            'content' => '认购单创建成功，编号为：'.$model->sn,
        ]);
        self::commit();
        return $model->id;
    }

    //保存商品信息
    public static function saveReqGoodsInfo($data)
    {

            if(isset($data['reqline_id']) && $data['reqline_id']){
                //更新
                $model = new ReqLine();
                $model->where('id',$data['reqline_id']);
                $model->updated_by = $data['curr_user_id'];
            }else{
                $user = UserService::getInfoById(['id'=>$data['curr_user_id']]);
                //新增
                $model = new ReqLine();
                $model->client_id = $user['client_id'];
                $model->org_id = $user['org_id'];
                $model->created_by = $user['id'];
                $model->updated_by = $user['id'];
            }
            $model->goods_id = $data['goods_id'];
            $model->req_id = $data['req_id'];
//            $model->goods_name = $data['goods_name'];
            $model->goods_price = $data['goods_price'];
            $model->goods_num = $data['goods_num'];
            $model->line_amt = $data['goods_num']*$data['goods_price'];
            $r = $model->save();

        return $r;
    }

    //冻结认购单
    public static function remove($params)
    {
        if (!empty($params['id'])) {
            self::beginTransaction();
            $res = Req::where('id',$params['id'])->update([
                'is_active' => '0'
            ]);
            $r = ReqLine::where('req_id',$res['id'])->update([
                'is_active' => '0'
            ]);
            self::commit();
            return $r;
        }
        return [];
    }

    //订单状态审核
    public static function editState($params)
    {
        if (!empty($params['req_id'])) {
            $res = Req::where('id',$params['req_id'])->update([
                'doc_state' => $params['doc_state']
            ]);
            return $res;
        }
        return [];
    }
}